import pandas as pd
import json

# 读取数据
user_df = pd.read_csv(r'/Users/liuyihua/Code/p5_cmcc/filter_data_csv/100/user.csv')
call_df = pd.read_csv(r'/Users/liuyihua/Code/p5_cmcc/filter_data_csv/100/call.csv')

# 处理 user 表
user_new = pd.DataFrame()
# 将16进制字符串转换为整数
user_new['MSISDN'] = user_df['MSISDN'].apply(lambda x: int(x, 16))
# 创建 properties 列，存储原始 MSISDN 的 JSON 格式
user_new['properties'] = user_df['MSISDN'].apply(lambda x: json.dumps({'original_msisdn': str(x)}))

# 处理 call 表
call_new = pd.DataFrame()
# 将16进制字符串转换为整数
call_new['MSISDN'] = call_df['MSISDN'].apply(lambda x: int(x, 16))
call_new['OPP_MSISDN'] = call_df['OPP_MSISDN'].apply(lambda x: int(x, 16))
# 创建 properties 列，存储 STATIS_YMD 和 PROV_ID 的 JSON 格式
call_new['properties'] = call_df.apply(
    lambda row: json.dumps({
        'STATIS_YMD': str(row['STATIS_YMD']),
        'PROV_ID': str(row['PROV_ID'])
    }), 
    axis=1
)

# 保存结果
user_new.to_csv('user_new.csv', index=False)
call_new.to_csv('call_new.csv', index=False)

# 显示处理结果
print("User 表处理结果：")
print(user_new.head())
print(f"\nUser 表形状: {user_new.shape}")

print("\n" + "="*50)
print("\nCall 表处理结果：")
print(call_new.head())
print(f"\nCall 表形状: {call_new.shape}")